package cn.net.withub.dataCollector.fastdfs.service;


import cn.net.withub.dataCollector.fastdfs.*;
import cn.net.withub.dataCollector.fastdfs.model.NameValuePair;
import org.apache.log4j.Logger;

import java.io.File;

public class FileManager implements FileManagerConfig {
    private static Logger log = Logger.getLogger(FileManager.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1556733247831388789L;
	private static TrackerClient trackerClient;
    private static TrackerServer trackerServer;
    private static StorageServer storageServer;
    private static StorageClient storageClient;

    static {
        try {
            String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();

            String fdfsClientConfigFilePath = classPath + File.separator + CLIENT_CONFIG_FILE;
            ClientGlobal.init(fdfsClientConfigFilePath);

            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient = new StorageClient(trackerServer, storageServer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * <strong>文件上传</strong>
     * 
     */
    public static String upload(FastDFSFile file,NameValuePair[] valuePairs) {
        String[] uploadResults = null;
        try {
            //log.info(file.getContent().toString());
            //log.info(file.getExt());
            //log.info(valuePairs);
            uploadResults = storageClient.upload_file(file.getContent(),file.getExt(), valuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String groupName = uploadResults[0];
        String remoteFileName = uploadResults[1];

        String fileAbsolutePath = PROTOCOL
                + ClientGlobal.TRACKER_NGNIX_ADDR
                //+ trackerServer.getInetSocketAddress().getHostName()
                //+ SEPARATOR + TRACKER_NGNIX_PORT 
                + SEPARATOR + groupName
                + SEPARATOR + remoteFileName;
        //log.info("upload-url:"+fileAbsolutePath);
        return fileAbsolutePath;
    }
    /**
     * <strong>文件上传</strong>
     *
     */
    public static String upload2(FastDFSFile file,NameValuePair[] valuePairs) {
        String[] uploadResults = null;
        try {
            //log.info(file.getContent().toString());
            //log.info(file.getExt());
            //log.info(valuePairs);
            TrackerClient trackerClient2 = null;
            TrackerServer trackerServer2 = null;
            StorageClient storageClient2 = null;
            String classPath = new File(FileManager.class.getResource("/").getFile()).getCanonicalPath();

            String fdfsClientConfigFilePath2 = classPath + File.separator + CLIENT_CONFIG_FILE;
            ClientGlobal.init(fdfsClientConfigFilePath2);
            trackerClient2 = new TrackerClient();
            trackerServer2 = trackerClient2.getConnection();
            storageClient2 = new StorageClient(trackerServer2, storageServer);
            uploadResults = storageClient2.upload_file(file.getContent(),file.getExt(), valuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try{
                if(null != storageServer)
                    storageServer.close();
            }catch (Exception e){
                e.printStackTrace();

            }finally {
                storageServer = null;
            }
        }
        String groupName = "";
        if(null != uploadResults && uploadResults.length > 0)
            groupName = uploadResults[0] == null ? "" : uploadResults[0];

        String remoteFileName = "";
        if(null != uploadResults && uploadResults.length > 0)
            remoteFileName = uploadResults[1] == null ? "" : uploadResults[1];

        String fileAbsolutePath = PROTOCOL
                + ClientGlobal.TRACKER_NGNIX_ADDR
                //+ trackerServer.getInetSocketAddress().getHostName()
                //+ SEPARATOR + TRACKER_NGNIX_PORT
                + SEPARATOR + groupName
                + SEPARATOR + remoteFileName;
        //log.info("upload-url:"+fileAbsolutePath);
        return fileAbsolutePath;
    }
    /**
     * <strong>文件下载</strong>
     * 
     */
    public static byte[] download(String groupName,
            String remoteFileName,String specFileName) {
        byte[] content = null;
        
        try {
            content = storageClient.download_file(groupName, remoteFileName);
           
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return content;
    }


}
